﻿<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<!--
CODEPROJECT.AI SERVER MODULE EXPLORER

This page provides the means to test this module using the same infrastructure as
the CodeProject.AI Server explorer. This page also provides the UI elements that
the explorer will parse and use to build up the UI of the main explorer itself.

RULES AND CONVENTIONS

1. This page should provide sufficient functionality to test and explore this
   module.

2. This page should use the functionality in the explorer.js file so that when
   the elements of this page are inserted into the main explorer, it all works
   seamlessly. Specifically, you will probably use

   - clearImagePreview:         Clears the image preview area.
   - previewImage:              Displays an image in the shared image preview area and takes a
                                input[type=file] as parameter.
   - submitRequest:             Sends a request to the AI server.
   - setResultsHtml:            Sets the HTML in the shared 'results' element. Parameter is the HTML
                                to display.
   - getProcessingMetadataHtml: Gets HTML representing the common data returned from a call to a
                                module.
   - displayBaseResults:        Displays the common data returned from a call to a module.
   - showPredictionSummary:     Displays in the shared HTML results pane the list of predictions 
                                returned from an inference operation.
   - clearImageResult:          Clears the image result area 
   - showResultsImageData:      Displays an image in the shared image results area using the data
                                returned from a call to a module, and overlays bounding boxes if
                                present in the data
   - showResultsBoundingBoxes:  Displays bounding boxes on the shared image results area based on
                                the boxes returned in the predictions parameter. The first param is
                                an array of predictions returned from a computer vision operation.

3. There are 3 parts of this page that will be pulled into the main explorer
   during runtime: The HTML, the script, and the CSS. These sections are bounded by

    - HTML:   START EXPLORER MARKUP / END EXPLORER MARKUP pair, each within HTML comment brackets
    - Script: START EXPLORER SCRIPT / END EXPLORER SCRIPT pair, each as a // comment on its own line
    - CSS:    START EXPLORER STYLE  / END EXPLORER STYLE pair, each inside /* ... */ comments

   These delimiters should be on a line by themselves

4. **Please provide output elements to display the results of operations** if
   you wish to use the standard HTML / Image results elements in the main explorer
   
   - For HTML output, include a DIV with id 'results'
   - For Image preview, include an IMG element with id imgPreview
   - For image results, include an IMG with element imgPreview and a DIV with
     id 'imageMask'.
   - You can use a single image for both preview and results if you wish by only
     including a imgPreview image. Make sure you have the imageMask DIV though.
   - For Sound preview, include an AUDIO element with id 'snd' that contains a
     SOURCE tag

5. When this file is parsed and injected into the larger explorer, the HTML is
   injected first, then the script, then the CSS.

6. **To ensure uniqueness of elements** you can include the _MID_ macro in any
   name. This will be expanded to be [ModuleId]_ where [ModuleId] is the literal
   ID of this module. For instance <div id="_MID_TextBox"> becomes <div id="MyModuleId_TextBox">
-->
<head>
    <meta charset="utf-8" />
    <title>Object Detection (YOLOv5 .NET) Module Test</title>

    <link id="bootstrapCss" rel="stylesheet" type="text/css" href="http://localhost:32168/assets/bootstrap-dark.min.css">
    <link rel="stylesheet" type="text/css" href="http://localhost:32168/assets/server.css?v=2.5.0.0">
    <script type="text/javascript" src="http://localhost:32168/assets/server.js"></script>
    <script type="text/javascript" src="http://localhost:32168/assets/explorer.js"></script>

    <style>
/* START EXPLORER STYLE */
/* END EXPLORER STYLE */
    </style>

</head>
<body class="dark-mode">
<div class="mx-auto" style="max-width: 800px;">
    <h2 class="mb-3">Object Detection (YOLOv5 .NET) Module Test</h2>
    <form method="post" action="" enctype="multipart/form-data" id="myform">

<!-- START EXPLORER MARKUP -->
        <div class="form-group row">
            <label class="col-form-label col-2">Image</label>
            <input id="_MID_image" class="col form-control btn-light" type="file" style="width:17rem"
                   onchange="return previewImage(this)" />
            <input id="_MID_things" class="form-control btn-success" type="button" value="Detect Objects"
                   style="width:11rem" onclick="_MID_onDetectThings(_MID_image)"/>
        </div>
<hr>
        <div class="form-group row">
            <label class="col-form-label col-2">Image</label>
            <input id="_MID_customimage" class="col form-control btn-light" type="file"
                   style="width:17rem" onchange="return previewImage(this)" />
            <input id="_MID_customthings" class="form-control btn-warning" type="button"
                   value="Custom Detect" style="width:11rem" 
                   onclick="_MID_onCustomDetect(_MID_customModel.value, _MID_customimage)"/>
        </div>

        <div class="form-group d-flex justify-content-end mt-2">
            <label class="col-form-label col-2">Model</label>
            <select id="_MID_customModel" class="form-select" style="width:19rem">
                <option value="ipcam-general">General IP Cam</option>
                <option value="license-plate">License Plates v2</option>
                <option value="openlogo">Open Logo</option>
                <option value="actionnetv2">(Human) ActionNet v2</option>
            </select>
            <label style="width:11rem"></label>
        </div>
<!-- END EXPLORER MARKUP -->

        <div class="w-100 position-relative form-control my-4 p-0">
            <div id="imgMask" class="position-absolute"
                    style="left:0;top:0;pointer-events:none;z-index:10"></div>
            <img src="" id="imgPreview" class="w-100" style="height:250px;visibility:hidden">
        </div>
        <div>
            <h2>Results</h2>
            <div id="results" name="results" class="bg-light p-3" style="min-height: 100px;"></div>
        </div>

    </form>

    <script type="text/javascript">
// START EXPLORER SCRIPT
		async function _MID_onDetectThings(fileChooser) {

			clearImagePreview();

			if (fileChooser.files.length == 0) {
				alert("No file was selected for vision detection");
				return;
			}

			showPreviewImage(fileChooser.files[0]);
			let images = [fileChooser.files[0]];

			setResultsHtml("Detecting objects...");
			let data = await submitRequest('vision', 'detection', images, null);
            if (data) {
				showResultsBoundingBoxes(data.predictions);
				showPredictionSummary(data)
			};
		}

		async function _MID_onCustomDetect(model_name, fileChooser) {

			clearImagePreview();

			if (fileChooser.files.length == 0) {
				alert("No file was selected for custom object detection");
				return;
			}

			showPreviewImage(fileChooser.files[0]);
			let images = [fileChooser.files[0]];

			setResultsHtml(`Detecting objects using model ${model_name}...`);
			let data = await submitRequest('vision', 'custom/' + model_name, images, null);
            if (data) {
				showResultsBoundingBoxes(data.predictions);
				showPredictionSummary(data)
			};
		}

		async function _MID_getCustomModelList() {

			let data = await submitRequest('vision', 'custom/list', null, null);
            if (data?.models?.length) {
                let options = [];
                for (let i = 0; i < data.models.length; i++)
                    options.push({ name: data.models[i], value: data.models[i]});

                return options;
            }
            
            return null;
		}

        async function _MID_setCustomModelList() {

            let customModelList = await _MID_getCustomModelList();
            if (!customModelList) customModelList = [];
            setModelList("_MID_customModel", customModelList);
        }        


        // =====================================================================
        // Startup

        // Regularly update the custom list dropdown
        _MID_setCustomModelList();                                             // Initial update
        setTimeout(_MID_setCustomModelList, serverWarmupSec * 1000);           // ...In case warm up time needed
        setInterval(_MID_setCustomModelList, customModelUpdateFreqSec * 1000); // ...and now regular updates
        
        // Use our custom model list method to populate the main explorer's benchmark custom list
        GetBenchmarkCustomModelList = _MID_getCustomModelList;

// END EXPLORER SCRIPT
    </script>
</div>
</body>
</html>